package com.gtq.uhome.controller;

import com.gtq.uhome.pojo.Site;
import com.gtq.uhome.result.Result;
import com.gtq.uhome.service.SiteService;
import com.gtq.uhome.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: Zhong Ranjie
 * @create: 2020-08-03 17:22
 **/
@RestController
@RequestMapping("/site")
@Api(tags = "站点接口")
@CrossOrigin(origins = "*", maxAge = 3600)
public class SiteController {


    @Autowired
    private SiteService siteService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增站点接口", notes = "新增站点")
    public Result addNewSite(@RequestBody SiteVo site) {
        return siteService.addNewSite(site);
    }

    @PostMapping(value = "/search")
    @ApiOperation(value = "查询站点", notes = "根据站点名称模糊查询")
    public Result<List<Site>> searchSite(@RequestBody Map<String, String> siteName) {
        return Result.ok(siteService.searchSite(siteName.get("siteName")), "查询成功");
    }

    @PostMapping(value = "/addSiteMember")
    @ApiOperation(value = "添加站点员", notes = "新增站点员")
    public Result addSiteMember(@RequestBody AddSiteMemberVo siteMemberVo) {
        return siteService.addSiteMember(siteMemberVo);
    }

    @PostMapping("/updateMember")
    @ApiOperation(value = "编辑站点员", notes = "编辑姓名、电话、站点")
    public Result updateSiteMember(@RequestBody SiteMemberUpdateVo siteMemberVo) {
        boolean b = siteService.updateSiteMember(siteMemberVo);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.failed("修改失败");
    }

    @PostMapping("/deleteMember")
    @ApiOperation(value = "删除站点员")
    public Result deleteSiteMember(@RequestBody List<Integer> idList) {
        boolean b = siteService.deleteSiteMember(idList);
        if (b) {
            return Result.ok("删除成功");
        }
        return Result.failed("删除失败");
    }

    @PostMapping("/memberList")
    @ApiOperation(value = "查询站点员列表", notes = "根据站点id查询当前站点的站点员")
    public Result<List<SiteMemberVo>> findSiteMemberList(@RequestBody SearchMemberVo memberVo) {
        return Result.ok(siteService.findSiteMembers(memberVo.getSiteId(), memberVo.getName()), "查询成功");
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改站点", notes = "修改站点信息")
    public Result updateSite(@RequestBody SiteUpdateVo updateVo) {
        boolean b = siteService.updateSite(updateVo);
        if (b) {
            return Result.ok("修改成功");
        }
        return Result.failed("修改失败");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除站点", notes = "删除站点信息")
    public Result deleteSite(@RequestBody List<Integer> staffIdList) {
        boolean b = siteService.deleteSiteList(staffIdList);
        if (b) {
            return Result.ok("删除成功");
        }
        return Result.failed("删除失败");
    }
}
